package com.hay.user.service;

import com.hay.common.utils.NumberUtils;
import com.hay.user.mapper.UserMapper;
import com.hay.user.pojo.User;
import com.hay.user.utils.CodecUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @title: UserService
 * @Author HuangYan
 * @Date: 2020/7/31 21:51
 * @Version 1.0
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    // 设置验证码在redis中存入的前缀
    private static final String CODE_PREFIX = "user:verify:";

    public Boolean checkUser(String data, Integer type) {

        User user = new User();
        if (type == 1){
            user.setUsername(data);
        } else if (type == 2){
            user.setPhone(data);
        } else {
            return null;
        }
        return userMapper.selectCount(user) == 0;
    }

    /**
     * 发送验证码
     * @param phone
     */
    public void sendVerifyCode(String phone) {
        if (StringUtils.isBlank(phone)){
            return;
        }
        // 生成验证码
        String code = NumberUtils.generateCode(6);
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("code", code);
        // 发送验证码
        //amqpTemplate.convertAndSend("LEYOU-SMS-EXCHANGE", "leyou-sms-send", map);
        // 将验证码存入redis
        redisTemplate.opsForValue().set(CODE_PREFIX + phone, code, 5, TimeUnit.MINUTES);
    }

    /**
     * 用户注册
     * @param user
     * @param code
     */
    public Boolean userRegister(User user, String code) {

        // 验证验证码
        String redisCode = redisTemplate.opsForValue().get(CODE_PREFIX + user.getPhone());
        if (StringUtils.isEmpty(redisCode) || !StringUtils.equals(code, redisCode)){
            return false;
        }
        // 生成盐
        String salt = CodecUtils.generateSalt();

        // 加密密码
        String md5HexPassword = CodecUtils.md5Hex(user.getPassword(), salt);
        user.setPassword(md5HexPassword);

        // 存储数据
        user.setId(null);
        user.setSalt(salt);
        user.setCreated(new Date());
        int i = userMapper.insert(user);
        return i == 1;
    }

    /**
     * 查询用户
     * @param username
     * @param password
     * @return
     */
    public User queryUser(String username, String password) {
        User record = new User();
        record.setUsername(username);
        User user = userMapper.selectOne(record);

        if (user == null){
            return null;
        }

        String md5HexPassword = CodecUtils.md5Hex(password, user.getSalt());
        if (!StringUtils.equals(user.getPassword(), md5HexPassword)){
            return null;
        }
        return user;
    }
}
